package com.cn.demo4.service.impl;
import com.cn.demo4.entity.UserEntity;
import com.cn.demo4.mapper.UserMapper;
import com.cn.demo4.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserEntity> findAll(UserEntity userEntity) {

        return userMapper.findAll(userEntity);
    }

    @Override
    public String login(UserEntity userEntity) {
        if (userEntity.getCode().equalsIgnoreCase(userEntity.getSessionCode())) {
            //2.判断手机号和密码是否相等
            UserEntity model = userMapper.findByPhoneAndPassword(userEntity);
            System.out.println(model + "++++++");
            return model == null ? "" : "success";
        } else {
            return "codeError";
        }
    }

    @Override
    public UserEntity findById(Integer id) {
        return userMapper.findById(id);
    }


    @Override
    public String edit(UserEntity userEntity) {
        /**
         * 1.判断当前用户是否存在
         * 2.如果存在则提示用户重新注册
         * 3.如果不存在则直接新增数据，提示修改成功
         */
        //通过用户名来判断是否为true或者为false
        boolean bool = userMapper.findByUserName(userEntity.getUserName()) != null;
        if (bool) {
            //若bool为true则表示当前用户已经被注册
            return "isHave";
        } else {
            //JDBC返回的时新增的条数
            int result = userMapper.edit(userEntity);
            //三元运算符判断数据新增是否成功，成功result==1，返回success，失败result==0，返回error
            return result == 1 ? "success" : "error";
        }
    }

    @Override
    public String editPassword(UserEntity userEntity) {
        return userMapper.editPassword(userEntity);
    }

    @Override
    public int del(Integer id) {
        return userMapper.del(id);
    }


    @Override
    public String add(UserEntity userEntity) {
        //通过用户名来判断是否为true或者为false
        boolean bool = userMapper.findByUserName(userEntity.getUserName()) !=null;
        if (bool) {
            //若bool为true则表示当前用户已经被注册
            return "isHave";
        } else {
            //JDBC返回的时新增的条数
            int result = userMapper.add(userEntity);
            //三元运算符判断数据新增是否成功，成功result==1，返回success，失败result==0，返回error
            return result == 1 ? "success" : "error";
        }
    }

    @Override
    public UserEntity findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    public int getCount(UserEntity userEntity) {
        return userMapper.getCount(userEntity);
    }
}
